/**
 * Created by user on 2017/12/21.
 */
  import { Request } from './Global'
class ReplayPlayer{
  constructor() {
    this.innerAudioContext = null
    this.loaded = false
    this.restartTimer = null
  }
  createAudio(url) {
    var audio = new Audio()
    audio.src = url
    this.innerAudioContext = audio
  }
  startLoad() {
    var that = this
    return new Promise((resolve, reject) => {
      var loaded = false
      var timer = null
      function onupdate() {
        if (that.innerAudioContext.currentTime > 0 && !loaded) {
          loaded = true
          that.innerAudioContext.removeEventListener('timeupdate', onupdate, false)
          timer && clearTimeout(timer)
          that.innerAudioContext.pause()
          resolve(true)
        }
      }
      that.innerAudioContext.addEventListener('timeupdate', onupdate, false)
      that.innerAudioContext.play()
      timer = setTimeout(function() {
        if (!loaded) {
          that.innerAudioContext.pause()
          that.innerAudioContext.removeEventListener('timeupdate', onupdate, false)
          resolve(false)
        }
      }, 20 * 1000)
    })
  }
  async load(_url, seekTime, loadCallback, endCallback, loadError){
    var url = _url + '?v=' + Date.now()
    // if (!this.innerAudioContext) {
    //
    // }
    this.createAudio(url)
    var loaded = await this.startLoad()
    if (!loaded) {
      loadError && loadError()
      return
    }
    loadCallback && loadCallback()
    this.innerAudioContext.addEventListener('ended', endCallback, false)
  }
  restart(callback) {
    this.innerAudioContext.play()
    this.restartTimer && clearInterval(this.restartTimer)
    var time = parseInt(this.getCurrentTime())
    this.restartTimer = setInterval(() => {
      if (time != this.audio_time) {
        callback && callback()
        this.restartTimer && clearInterval(this.restartTimer)
      }
    },10)
  }
  start() {
    this.innerAudioContext.play()
  }
  pause() {
    try {
      this.innerAudioContext.pause()
    } catch (e) {

    }
  }
  async seekTo(pos, seekCallback) {
    var seeked = await this.seekPromise(pos)
    if (seeked) {
      seekCallback && seekCallback()
    }
  }
  seekPromise(pos) {
    var that = this
    return new Promise((resolve, reject) => {
      var lastTime = that.innerAudioContext.currentTime
      var isBefore = lastTime - pos > 0
      function removeListener() {
        that.innerAudioContext.addEventListener('timeupdate', timeUpdate, false)
      }
      function timeUpdate() {
        var current = that.innerAudioContext.currentTime
        if (isBefore) {
          if (current > pos && current < lastTime) {
            resolve(true)
            removeListener()
          }
        } else {
          if (current > pos) {
            resolve(true)
            removeListener()
          }
        }
      }
      that.innerAudioContext.addEventListener('timeupdate', timeUpdate, false)
      that.innerAudioContext.currentTime = pos
      if (that.innerAudioContext.paused) {
        that.innerAudioContext.play()
      }
    })
  }
  getCurrentTime() {
    var t = 0
    try {
      t = this.innerAudioContext.currentTime
    } catch (e) {}
    return t
  }
}
module.exports = new ReplayPlayer()
